package queue;
//基于单链表实现队列
public class MyQueue {
    static class Node {
        public String val;
        public Node next;

        public Node(String val) {
            this.val = val;
            this.next = null;
        }
    }

    private Node head = null;
    private Node tail = null;

    //基于链表实现队列
    //入队-》尾插
    public void offer(String val) {
    Node newNode = new Node(val);
    if(head==null){
        head=tail=newNode;
        return ;
    }else{

        tail.next=newNode;
        //尾插之后，更新tail
        tail=newNode;
    }
    }

    //出队-》头删
    public String poll() {
        if(head==null){
            return null;
        }
        //保存头节点的值
        //接下来把这个值删掉，需要返回这个值
        String val=head.val;
        head=head.next;
        if(head==null){
            //如果头节点已经是最后一个节点，则更新tail
            tail=null;
        }
        return val;
    }

    //查看队首元素
    public String peek() {
        if(head==null){
            return null;
        }else{
            //直接返回头节点的值
            return head.val;
        }
    }
    public boolean isEmpty(){
        return head==null;
    }
    public int size() {
      int  size=0;
      for(Node cur=head;cur!=null;cur=cur.next){
          size++;
      }
      return size;
    }
    public static void main(String[] args) {
          MyQueue queue=new MyQueue();
          queue.offer("a");
          queue.offer("b");
          queue.offer("c");
          System.out.println(queue.peek());//查看队首元素
          System.out.println(queue.poll());//出队
          System.out.println(queue.peek());//查看队首元素
          System.out.println(queue.poll());//出队
          System.out.println(queue.peek());//查看队首元素
          System.out.println(queue.poll());//出队
          System.out.println(queue.peek());//查看队首元素
          System.out.println(queue.isEmpty());//判断是否为空
    }
}